#!/usr/env/env ruby

require 'logger'
require 'wpspider/common/db_connector'
require 'wpspider/common/wppage'
require 'wpspider/importer/wppage_vo'
require 'wpspider/importer/xml_parser'

$KCODE = 'u'
$logger = Logger.new('import_log.txt')
$logger.level = Logger::DEBUG

module WPSpider
  module Importer

    #
    # ImportCommand class.
    #
    class ImportCommand
      include WPSpider::Common
      # Config file.
      attr_accessor :conffile
      # Input XML file.
      attr_accessor :xmlfile

      #
      # Constructor.
      #
      def initialize()
        @conffile = nil
        @xmlfile = nil
      end

      #
      # Executes command.
      #
      def execute()
        db_connector = DBConnector.new()
        db_connector.load_conffile(@conffile)
        db_connector.connect()

        xml_parser = XMLParser.new(@xmlfile)
        xml_parser.parse() do |wppage_vo|
          begin
            wppage = WPPage.new()
            wppage.name = wppage_vo.name
            wppage.size = wppage_vo.size
            wppage.words = wppage_vo.words.join("<>")
            wppage.time = Time.now()
            $logger.debug(wppage.inspect)
            #wppage.save()
          rescue Exception
            $logger.warn($!)
          end
        end
      end
    end
  end
end
